{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成绩等级为：A\n"
     ]
    }
   ],
   "source": [
    "# 1. 输入某年某月某日，判断这一天是这一年的第几天\n",
    "def day_of_year():\n",
    "    year = int(input(\"请输入年份: \"))\n",
    "    month = int(input(\"请输入月份: \"))\n",
    "    day = int(input(\"请输入日期: \"))\n",
    "    days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    if (year % 4 == 0 and year % 100!= 0) or year % 400 == 0:\n",
    "        days_in_month[2] = 29\n",
    "    total_days = sum(days_in_month[:month]) + day\n",
    "    print(f\"{year}年{month}月{day}日是这一年的第{total_days}天\")\n",
    "\n",
    "# 2. 判断1000中有多少个素数，并输出所有素数\n",
    "def primes_up_to_1000():\n",
    "    primes = []\n",
    "    for num in range(2, 1001):\n",
    "        if all(num % i != 0 for i in range(2, int(num**0.5) + 1)):\n",
    "            primes.append(num)\n",
    "    return primes, len(primes)\n",
    "\n",
    "# 3. 利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示\n",
    "def grade_to_letter(score):\n",
    "    return 'A' if score >= 90 else ('B' if 60 <= score < 90 else 'C')\n",
    "\n",
    "# 4. 输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数\n",
    "def count_characters(string):\n",
    "    letters = 0\n",
    "    spaces = 0\n",
    "    digits = 0\n",
    "    others = 0\n",
    "    for char in string:\n",
    "        if char.isalpha():\n",
    "            letters += 1\n",
    "        elif char.isspace():\n",
    "            spaces += 1\n",
    "        elif char.isdigit():\n",
    "            digits += 1\n",
    "        else:\n",
    "            others += 1\n",
    "    return letters, spaces, digits, others\n",
    "\n",
    "# 5. 一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数\n",
    "def perfect_numbers_up_to_1000():\n",
    "    perfect_numbers = []\n",
    "    for num in range(1, 1001):\n",
    "        factors = [i for i in range(1, num) if num % i == 0]\n",
    "        if sum(factors) == num:\n",
    "            perfect_numbers.append(num)\n",
    "    return perfect_numbers\n",
    "\n",
    "# 6. 输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n",
    "def factorial_sum(N):\n",
    "    factorial_sum = 0\n",
    "    for i in range(1, N + 1):\n",
    "        factorial = 1\n",
    "        for j in range(1, i + 1):\n",
    "            factorial *= j\n",
    "        factorial_sum += factorial\n",
    "    return factorial_sum\n",
    "\n",
    "# 7. 输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "def print_diamond(N):\n",
    "    for i in range(1, N + 1, 2):\n",
    "        print('*' * i)\n",
    "    for i in range(N - 2, 0, -2):\n",
    "        print('*' * i)\n",
    "\n",
    "# 8. 对10个数进行排序\n",
    "def sort_numbers(numbers):\n",
    "    return sorted(numbers)\n",
    "\n",
    "# 9. 有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中\n",
    "def insert_into_sorted_array(sorted_array, number):\n",
    "    for i in range(len(sorted_array)):\n",
    "        if number < sorted_array[i]:\n",
    "            return sorted_array[:i] + [number] + sorted_array[i:]\n",
    "    return sorted_array + [number]\n",
    "\n",
    "# 10. 有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位\n",
    "def last_person_standing(n):\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)\n",
    "        people.pop(index)\n",
    "    return people[0]\n",
    "\n",
    "# 选择构架\n",
    "def main():\n",
    "    choice = input(\"请输入题目编号（1-10内的数）：\")\n",
    "    \n",
    "    if choice == '1':\n",
    "        year, month, day = map(int, input(\"请输入年 月 日（空格分隔）：\").split())\n",
    "        print(f\"{year}年{month}月{day}日是这一年的第{day_of_year(year, month, day)}天\")\n",
    "    \n",
    "    elif choice == '2':\n",
    "        primes, count = primes_up_to_1000()\n",
    "        print(f\"1000以内有{count}个素数，分别是：{primes}\")\n",
    "    \n",
    "    elif choice == '3':\n",
    "        score = float(input(\"请输入学习成绩：\"))\n",
    "        print(f\"成绩等级为：{grade_to_letter(score)}\")\n",
    "    \n",
    "    elif choice == '4':\n",
    "        string = input(\"请输入一行字符串：\")\n",
    "        letters, spaces, digits, others = count_characters(string)\n",
    "        print(f\"字母：{letters}，空格：{spaces}，数字：{digits}，其它字符：{others}\")\n",
    "    \n",
    "    elif choice == '5':\n",
    "        perfect_numbers = perfect_numbers_up_to_1000()\n",
    "        print(f\"1000以内的完数有：{perfect_numbers}\")\n",
    "    \n",
    "    elif choice == '6':\n",
    "        N = int(input(\"请输入任意数N：\"))\n",
    "        print(f\"1+2!+3!+...+{N}!的值为：{factorial_sum(N)}\")\n",
    "    \n",
    "    elif choice == '7':\n",
    "        N = int(input(\"请输入任意奇数N：\"))\n",
    "        if N % 2 == 0 or N < 1:\n",
    "            print(\"请输入一个奇数！\")\n",
    "        else:\n",
    "            print_diamond(N)\n",
    "    \n",
    "    elif choice == '8':\n",
    "        numbers = list(map(int, input(\"请输入10个数（空格分隔）：\").split()))\n",
    "        sorted_numbers = sort_numbers(numbers)\n",
    "        print(f\"排序后的数组为：{sorted_numbers}\")\n",
    "    \n",
    "    elif choice == '9':\n",
    "        sorted_array = list(map(int, input(\"请输入已排好序的数组（空格分隔）：\").split()))\n",
    "        number = int(input(\"请输入要插入的数：\"))\n",
    "        new_array = insert_into_sorted_array(sorted_array, number)\n",
    "        print(f\"插入后的数组为：{new_array}\")\n",
    "    \n",
    "    elif choice == '10':\n",
    "        n = int(input(\"请输入人数n：\"))\n",
    "        print(f\"最后留下的是原来第{last_person_standing(n)}号的那位\")\n",
    "    \n",
    "    else:\n",
    "        print(\"\")\n",
    "\n",
    "# 运行选择构架\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
